package com.android.common.base.rv;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.android.common.R;
import com.android.common.mvp.model.BaseMultiSelectBean;

public class BaseMultiSelectRvHolder<T extends BaseMultiSelectBean> extends BaseRvHolder<T>
    implements CompoundButton.OnCheckedChangeListener{

    private final CheckBox mCheckBox;

    public BaseMultiSelectRvHolder(View itemView) {
        super(itemView);
        mCheckBox = itemView.findViewById(R.id.cb_select);
        initEvent();
    }

    private void initEvent(){
        mCheckBox.setOnCheckedChangeListener(this);
    }

    @Override
    public void bindData(T t, int position) {
        if (t.isVisible()){
            mCheckBox.setVisibility(View.VISIBLE);
        }else {
            mCheckBox.setVisibility(View.GONE);
        }
        mCheckBox.setChecked(t.isSelected() && t.isVisible());
    }

    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean checked){
        if (null != mChildViewCheckedListener && checkBox.isPressed()){
            mChildViewCheckedListener.onSwitchChanged(checkBox,getAbsoluteAdapterPosition(),checked);
        }
    }
}
